package com.cloudlead.shipownerAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.common.worker.client.exception.RestClientException;
import com.cloudlead.commonData.service.BaseShipService;
import com.cloudlead.commonData.vo.BaseCompany;
import com.cloudlead.commonData.vo.BaseShip;
import com.cloudlead.shipownerAction.service.BaseShipEditSer;
import com.cloudlead.shipownerService.vo.BaseShipVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 20150817 on 2015/9/2.
 */
@Service
public class BaseShipEditSerImpl implements BaseShipEditSer {
    @Autowired
    BaseShipService baseShipService;

    //船舶创建
    @Override
    public void createShip(BaseShipVo vo) {
        BaseShip  baseShip=new BaseShip();
        baseShip.setName(vo.getName());
        if(baseShipService.findOne(baseShip)!=null){
            throw new RestClientException("该船名已存在！");
        }
            BaseShip bs = new BaseShip();
            BeanUtils.copyProperties(vo,bs);
            if(vo.getBaseCompany()!=null&&!vo.getBaseCompany().equals("")){//如果公司不为空
                BaseCompany bc = new BaseCompany();
                bc.setId(vo.getBaseCompany());
                bs.setBaseCompany(bc);
            }
            bs.setComments(vo.getComments());
            bs.setTonnage(vo.getTonnage());
            bs.setFlag("0");
            bs.setStatus(vo.getStatus());
            bs.setBelonged(vo.getBelonged());
            bs.setLocation(vo.getLocation());
            bs.setMmsi(vo.getMmsi());
            bs.setName(vo.getName());
            bs.setShipOwnerId(vo.getShipOwnerId());
            baseShipService.create(bs);

    }

    //编辑船舶
    @Override
    public void editShip(BaseShipVo vo) {
        BaseShip  baseShip=new BaseShip();
        baseShip.setName(vo.getName());
        BaseShip baseShip1=baseShipService.findOne(baseShip);
        if(baseShip1!=null&&!baseShip1.getId().equals(vo.getId())){
            throw new RestClientException("船名"+vo.getName()+"已存在。");
        }else {
            BaseShip bs = baseShipService.view(vo.getId());
            BeanUtils.copyProperties(vo, bs);
            if (vo.getBaseCompany() != null && !vo.getBaseCompany().equals("")) {//如果公司不为空
                BaseCompany bc = new BaseCompany();
                bc.setId(vo.getBaseCompany());
                bs.setBaseCompany(bc);
            }
            bs.setComments(vo.getComments());
            bs.setTonnage(vo.getTonnage());
            bs.setFlag("0");
            bs.setStatus(vo.getStatus());
            bs.setBelonged(vo.getBelonged());
            bs.setLocation(vo.getLocation());
            bs.setMmsi(vo.getMmsi());
            bs.setName(vo.getName());
            bs.setShipOwnerId(vo.getShipOwnerId());
            baseShipService.update(bs);
        }
    }

    //单值查询
    @Override
    public Object findOneShip(BaseShipVo vo) {
        BaseShip bs = new BaseShip();
        BeanUtils.copyProperties(vo,bs);
        bs.setFlag("0");
        BaseShip bs1 = baseShipService.findOne(bs);
        return bs1;
    }

    //分页查询
    @Override
    public Page<BaseShipVo> findAllShip(BaseShipVo vo, int page, int rows) {
        BaseShipVo bso = new BaseShipVo();
        if(vo!=null){
            bso = vo;
        }
        PageRequest pr = new PageRequest();
        List<Term> tr = new ArrayList<Term>();
        tr.add(new Term("flag","0"));
        if(vo.getBaseCompany()!=null&&!vo.getBaseCompany().equals("")){//如果公司不为空
            tr.add(new Term("baseCompanyId",vo.getBaseCompany()));
        }
        if(vo.getMmsi()!=null&&!vo.getMmsi().equals("")){//如果公司不为空
            tr.add(new Term("mmsi",Operator.LK,"%"+vo.getMmsi()+"%"));
        }
        if(vo.getName()!=null&&!vo.getName().equals("")){//如果公司不为空
            tr.add(new Term("name",Operator.LK,"%"+vo.getName()+"%"));
        }
        Filter filter = new Filter(new Criteria(tr));
        pr.setSize(rows);
        pr.setPage(page);
        pr.setFilter(filter);
        Page<BaseShip> pbs = (Page<BaseShip>)baseShipService.findAll(pr);
        Page<BaseShipVo> pbo = new Page<BaseShipVo>();
        BeanUtils.copyProperties(pbs,pbo);
        return pbo;
    }

    //删除船舶
    @Override
    @Transactional//回滚
    public void delShip(String id) {
        BaseShip bs = baseShipService.view(id);//根据ID删除
        bs.setFlag("1");
        baseShipService.update(bs);
    }
}
